﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using DAL;
using TTCN_QLDiemC3.MyUserControl.UCHienThi;

namespace TTCN_QLDiemC3.MyUserControl.UCHienThi
{
    public partial class UCThemGiaoVien : UserControl
    {
        GiaoVienBUO gvBUO = new GiaoVienBUO();
        NumberBUO numberBUO = new NumberBUO();
        MonHocBUO mhBUO = new MonHocBUO();

        int id;
        public String id_GV, id_Khoi, id_MH;

        public UCThemGiaoVien()
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = gvBUO.HienThiGiaoVien();
            showKhoi();
            //showMonDay();
        }

        private void btnLamLai_Click(object sender, EventArgs e)
        {
            txtTenGV.Text = "";
            txtSDT.Text = "";
            txtEmail.Text = "";
            txtDiaChi.Text = "";
            cbxKhoi.Text = "";
            cbxMonHoc.Text = "";
        }

        private void showKhoi()
        {
            tbl_Khoi tbl_Khoi = (tbl_Khoi)cbxKhoi.SelectedItem;
            cbxKhoi.DataSource = gvBUO.getKhoi();
            cbxKhoi.DisplayMember = "TenKhoi";
            cbxKhoi.ValueMember = "id_Khoi";
        }

        private void showMonDay()
        {
            tbl_MonHoc tbl_MonHoc = (tbl_MonHoc)cbxMonHoc.SelectedItem;
            cbxMonHoc.DataSource = gvBUO.getMonDay(cbxKhoi.SelectedValue.ToString());
            cbxMonHoc.DisplayMember = "TenMonHoc";
            cbxMonHoc.ValueMember = "id_MonHoc";
        }

        private void cbxKhoi_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbxMonHoc.Enabled = true;
            showMonDay();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if ((txtDiaChi.Text == "") || (txtEmail.Text == "") || (txtSDT.Text == "") || (txtTenGV.Text == "") || (cbxMonHoc.Text == "") || (cbxKhoi.Text == ""))
            {
                MessageBox.Show("Chưa nhập đủ thông tin", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                if (numberBUO.isNumberInput(txtSDT.Text) == false)
                {
                    MessageBox.Show("Trường số điện thoại phải nhập số", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    tbl_MonHoc tbl_mh = new tbl_MonHoc();
                    tbl_mh.id_Khoi = cbxKhoi.SelectedValue.ToString();
                    tbl_mh.id_MonHoc = cbxMonHoc.SelectedValue.ToString();
                    tbl_mh.TenMonHoc = cbxMonHoc.SelectedItem.ToString();
                    if (mhBUO.Insert(tbl_mh))
                    {
                        tbl_GiaoVien tbl_gv = new tbl_GiaoVien();
                        tbl_gv.id_MonHoc = cbxMonHoc.SelectedValue.ToString();
                        tbl_gv.HoTenGV = txtTenGV.Text;
                        tbl_gv.NgaySinh = dateTimePicker1.Text;
                        tbl_gv.Email = txtEmail.Text;
                        tbl_gv.SDT = txtSDT.Text;
                        tbl_gv.DiaChi = txtDiaChi.Text;
                        if (gvBUO.Insert(tbl_gv))
                        {
                            dataGridView1.AutoGenerateColumns = false;
                            dataGridView1.DataSource = gvBUO.HienThiGiaoVien();
                            MessageBox.Show("Thêm giáo viên thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            UCThemGiaoVien a = new UCThemGiaoVien();
                            Form form = this.ParentForm;
                            ControlAccess.ShowConTrol(form, a, "grbHienThiChucNang");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Insert vào bảng tbl_MonHoc không thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
        }

        private void dataGridView1_MouseEnter(object sender, EventArgs e)
        {
            
        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {

                    DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);


                    // Only RowHeader and Cells (which make up a row) should fire a menu
                    if (hit.Type == DataGridViewHitTestType.RowHeader
                        || hit.Type == DataGridViewHitTestType.Cell
                        && hit.RowIndex >= 0)
                    {
                        // Notice how I assign the menu to the dataGridView below...

                        dataGridView1.ClearSelection();
                        dataGridView1.Rows[hit.RowIndex].Selected = true;
                        dataGridView1.ContextMenuStrip = contextMenuStrip1;
                        dataGridView1.ContextMenuStrip.Show(this.dataGridView1, new Point(e.X, e.Y));

                        id = dataGridView1.HitTest(e.X, e.Y).RowIndex;
                        id_GV = dataGridView1.Rows[id].Cells[0].Value.ToString();
                        id_Khoi = dataGridView1.Rows[id].Cells[1].Value.ToString();
                        id_MH = dataGridView1.Rows[id].Cells[2].Value.ToString(); 

                    }
                    // and then I remove the menu here...
                    // This is because after we assign it to the dataGridView
                    // it allows you to right-click anywhere on the control to
                    // get a menu.  Try removing this line and expirement.
                    dataGridView1.ContextMenuStrip = null;

                }
            }
            catch
            {
                contextMenuStrip1.Visible = false;
            }
        }

        private void danhSáchGiáoViênToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UCDanhSachGV a = new UCDanhSachGV();

            Form form = this.ParentForm;
            ControlAccess.ShowConTrol(form, a, "grbHienThiChucNang");
        }

        private void sửaGiáoViênToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UCSuaGiaoVien a = new UCSuaGiaoVien(id_GV, id_Khoi, id_MH);

            Form form = this.ParentForm;
            ControlAccess.ShowConTrol(form, a, "grbHienThiChucNang");
        }

        private void xóaGiáoViênToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult ds = MessageBox.Show("Bạn có chắc chắn muốn xóa", "Xóa dữ liệu giáo viên", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (ds == DialogResult.Yes)
            {
                if (gvBUO.Delete(id_GV))
                {
                    MessageBox.Show("Xóa giáo viên thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Xóa giáo viên thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                dataGridView1.DataSource = gvBUO.HienThiGiaoVien();
            }
        }
    }
}
